package com.zhou.JavaAndroidFirm.presenter;

import android.view.View;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.zhou.JavaAndroidFirm.R;
import com.zhou.JavaAndroidFirm.app.AppToast;
import com.zhou.JavaAndroidFirm.bean.CircleItem;
import com.zhou.JavaAndroidFirm.bean.CommentItem;
import com.zhou.JavaAndroidFirm.bean.CommonResponse;
import com.zhou.JavaAndroidFirm.bean.FavortItem;
import com.zhou.JavaAndroidFirm.bean.PageBean;
import com.zhou.JavaAndroidFirm.constract.CircleZoneContract;
import com.zhou.JavaAndroidFirm.constract.CommentConfig;
import com.zhou.JavaAndroidFirm.utils.AppCache;
import com.zhou.JavaAndroidFirm.utils.DatasUtil;
import com.zhou.JavaAndroidFirm.widget.DialogOnClickListener;
import com.zhou.JavaAndroidFirm.widget.GoodView;
import com.zhou.JavaAndroidFirm.widget.MDAlertDialog;

import org.json.JSONObject;

import java.util.List;
import java.util.Random;

import rx.Subscriber;

/**
 * @author Administrator
 * @date 2018/7/20 0020
 * @des
 */
public class CircleZonePresenter extends CircleZoneContract.Presenter {
    @Override
    public void getNotReadNewsCount() {
        addSubscribe(mModel.getZoneNotReadMsg().subscribe(new Subscriber<String>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(String s) {
                mView.updateNotReadNewsCount(10, s);
            }
        }));
    }

    @Override
    public void getListData(String type, String userId, final int page, int rows) {
        addSubscribe(mModel.getListDatas(type, userId, page, rows).subscribe(new Subscriber<CommonResponse>() {
            @Override
            public void onCompleted() {
            }

            @Override
            public void onError(Throwable e) {
                mView.showTip(e.getMessage());
            }

            @Override
            public void onNext(CommonResponse commonResponse) {
                try {
                    List<CircleItem> data = new Gson()
                            .fromJson(
                                    new JSONObject(commonResponse.getMsg()).getString("list"),
                                    new TypeToken<List<CircleItem>>() {
                                    }.getType());
                    for (int i = 0; i < data.size(); i++) {
                        data.get(i).setPictures(DatasUtil.getRandomPhotoUrlString(new Random().nextInt(9)));
                    }
                    PageBean pageBean = new Gson().fromJson(new JSONObject(commonResponse.getMsg()).getString("page"), PageBean.class);
                    mView.setListData(data, pageBean);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        }));
    }

    MDAlertDialog mdAlertDialog;

    @Override
    public void deleteCircle(final String circleId, final int position) {
        mdAlertDialog = new MDAlertDialog.Builder(mContext)
                .setHeight(0.25f)  //屏幕高度*0.3
                .setWidth(0.7f)  //屏幕宽度*0.7
                .setTitleVisible(true)
                .setTitleText("温馨提示")
                .setTitleTextColor(R.color.black_light)
                .setContentText("确定删除该条说说吗？")
                .setContentTextColor(R.color.black_light)
                .setLeftButtonText("不删除")
                .setLeftButtonTextColor(R.color.black_light)
                .setRightButtonText("删除")
                .setRightButtonTextColor(R.color.main_color)
                .setTitleTextSize(16)
                .setContentTextSize(14)
                .setButtonTextSize(14)
                .setOnclickListener(new DialogOnClickListener() {
                    @Override
                    public void clickLeftButton(View view) {
                        mdAlertDialog.dismiss();
                    }

                    @Override
                    public void clickRightButton(View view) {
                        mdAlertDialog.dismiss();
                        mView.startProgressDialog();
                        addSubscribe(mModel.deleteCircle(circleId, position).subscribe(new Subscriber<CommonResponse>() {
                            @Override
                            public void onCompleted() {
                                mView.stopProgressDialog();
                            }

                            @Override
                            public void onError(Throwable e) {
                                mView.stopProgressDialog();
                                AppToast.showToast(mContext.getString(R.string.net_error));
                            }

                            @Override
                            public void onNext(CommonResponse result) {
                                mView.update2DeleteCircle(circleId, position);
                            }
                        }));
                    }
                })
                .build();
        mdAlertDialog.show();
    }

    GoodView mGoodView;

    @Override
    public void addFavort(final String publishId, String publishUserId, final int circlePosition, final View view) {
        mView.startProgressDialog();
        addSubscribe(mModel.addFavort(publishId, publishUserId).subscribe(new Subscriber<CommonResponse>() {
            @Override
            public void onCompleted() {
                mView.stopProgressDialog();
            }

            @Override
            public void onError(Throwable e) {
                mView.stopProgressDialog();
                AppToast.showToast(mContext.getString(R.string.net_error));
            }

            @Override
            public void onNext(CommonResponse commonResponse) {
                if (null != commonResponse) {
                    if (mGoodView == null) {
                        mGoodView = new GoodView(mContext);
                    }
                    mGoodView.setImage(R.drawable.dianzan);
                    mGoodView.show(view);
                    FavortItem item = new FavortItem(publishId, AppCache.getInstance().getUserId(), "zhou");
                    mView.update2AddFavorite(circlePosition, item);
                }
            }
        }));
    }

    @Override
    public void deleteFavort(final String publishId, String publishUserId, final int circlePosition) {
        mView.startProgressDialog();
        addSubscribe(mModel.deleteFavort(publishId, publishUserId).subscribe(new Subscriber<CommonResponse>() {
            @Override
            public void onCompleted() {
                mView.stopProgressDialog();
            }

            @Override
            public void onError(Throwable e) {
                mView.stopProgressDialog();
                AppToast.showToast(mContext.getString(R.string.net_error));
            }

            @Override
            public void onNext(CommonResponse response) {
                if (null != response) {
                    mView.update2DeleteFavort(circlePosition, AppCache.getInstance().getUserId());
                }
            }
        }));
    }

    @Override
    public void addComment(final String content, final CommentConfig config) {
        if (null == config) {
            return;
        }
        mView.startProgressDialog();
        addSubscribe(mModel.addComment(config.getPublishUserId(), new CommentItem(config.getName(), config.getId(), content, config.getPublishId(), AppCache.getInstance().getUserId(), "zhou")).subscribe(new Subscriber<CommonResponse>() {
            @Override
            public void onCompleted() {
                mView.stopProgressDialog();
            }

            @Override
            public void onError(Throwable e) {
                mView.stopProgressDialog();
                AppToast.showToast(mContext.getString(R.string.net_error));
            }

            @Override
            public void onNext(CommonResponse response) {
                if (response != null) {
                    mView.update2AddComment(config.circlePosition, new CommentItem(config.getName(), config.getId(), content, config.getPublishId(), AppCache.getInstance().getUserId(), "zhou"));
                }
            }
        }));
    }

    @Override
    public void deleteComment(final int circlePosition, final String commentId, final int commentPosition) {
        mView.startProgressDialog();
        addSubscribe(mModel.deleteComment(commentId).subscribe(new Subscriber<CommonResponse>() {
            @Override
            public void onCompleted() {
                mView.stopProgressDialog();
            }

            @Override
            public void onError(Throwable e) {
                mView.stopProgressDialog();
                AppToast.showToast(mContext.getString(R.string.net_error));
            }

            @Override
            public void onNext(CommonResponse result) {
                mView.update2DeleteComment(circlePosition, commentId, commentPosition);
            }
        }));
    }

    @Override
    public void showEditTextBody(CommentConfig commentConfig) {
        mView.updateEditTextBodyVisible(View.VISIBLE, commentConfig);
    }
}
